package gspushmq.v1.save;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import ggframework.mongodb.BasicDBObject;
import ggframework.mongodb.DBCollection;
import ggframework.mongodb.GGMongoOperator;
import gspushmq.model.Data;
import gspushmq.model.Message;
import gspushmq.model.User;
import gspushmq.util.GGLogger;
import gspushmq.util.MongoUtil;

/**
 * 消息发送记录表
 *
 * @author yutao
 * @version 1.0
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2016 SunTime Co'Ltd Inc.All rights reserved.
 * @date 2018年11月6日下午5:18:56
 * @since JDK1.7
 */
public class SaveSendRecord {
	
	
    public static void save(Message message, Data data) {
    	if (message == null || data == null) {
            GGLogger.warn(SaveSendRecord.class, "【message】不能为null");
            return;
        }
        String mqType = message.getMq_type();

        switch (mqType) {
            case "api":
                doneApi(message, data);
                break;
            case "sys":
            case "live":
            case "hq":
            case "news":
                doneSys(message, data);
                break;
            default:
                doneApi(message, data);
                break;
        }
    }

    public static void save(Message message, Map<String, Object> map) {
    	doneMap(message, map);
    }
    
    public static void save(Data data, User user) {
    	doneUser(data, user);
    }

    private static void doneUser(Data data, User user){

        DBCollection noticeSendRecord = MongoUtil.getFtAccountBehaviorCollection("cabs_msg_send_record");
        BasicDBObject match = GGMongoOperator.newDBObject(noticeSendRecord);
        match.put("account_id", Long.valueOf(user.getAccountId()));
        match.put("account_name",user.getAccountName());
        match.put("uuid", data.getUuid());
        match.put("title", data.getTitle());
        match.put("content", data.getContent());
        match.put("img_url", data.getFileAddress());
        match.put("link_type", data.getLinkType());
        match.put("link_url", data.getLinkUrl());
        match.put("mq_type", data.getMqType());
        match.put("mq_sub_type", data.getMqSubType());
        match.put("product_line", Integer.valueOf(user.getProductLine()));
        match.put("module", data.getModule());
        match.put("target", data.getTarget());
        match.put("expire_date", data.getExpireDate());//过期时间
        match.append("insert_date", new Date()); //推送时间
        match.put("status", 1);

        //todo request_id checked
        match.put("request_id", null);
        noticeSendRecord.insert(match);
    }
    /**
     *
     * @author yutao
     * @date 2018年11月16日下午4:06:24
     */
    private static void doneMap(Message message, Map map) {
    	
    	DBCollection messSendRecord = MongoUtil.getFtAccountBehaviorCollection("cabs_msg_send_record");
    	
    	BasicDBObject match = new BasicDBObject(map);
    	//已发送
    	match.append("status", 1);
    	match.append("account_id", Long.valueOf(message.getAccount_id()));
    	match.append("client_flag", message.getClient_flag());
    	match.append("product_line", message.getProduct_line());
    	match.append("mq_type", message.getMq_type());
    	match.append("mq_sub_type", message.getMq_sub_type());
    	
    	match.append("insert_date", new Date());
    	messSendRecord.insert(match);
    }
	/**
     * 处理api日志
     *
     * @author yutao
     * @date 2018年11月16日下午4:06:24
     */
    private static void doneApi(Message message, Data data) {

        DBCollection messSendRecord = MongoUtil.getFtAccountBehaviorCollection("cabs_msg_send_record");

        BasicDBObject match = new BasicDBObject();
        //已发送
        match.append("status", 1);
        match.append("account_id", Long.valueOf(message.getAccount_id()));
        match.append("client_flag", message.getClient_flag());
        match.append("product_line", message.getProduct_line());
        match.append("mq_type", message.getMq_type());
        match.append("mq_sub_type", message.getMq_sub_type());

        match.append("uuid", data.getUuid());
        match.append("insert_date", new Date());
        messSendRecord.insert(match);
    }

    /**
     * 处理系统通知日志
     *
     * @author yutao
     * @date 2018年11月16日下午4:06:32
     */
    private static void doneSys(Message message, Data data) {
    	if(data == null) {
    		GGLogger.warn(SaveSendRecord.class, "【data】不能为null");
    		return;
    	}
    	
        String uuid = data.getUuid();
        if (StringUtils.isBlank(uuid)) {
            GGLogger.warn(SaveSendRecord.class, "【uuid】不能为null");
            return;
        }
    	
        DBCollection messSendRecord = MongoUtil.getFtAccountBehaviorCollection("cabs_msg_send_record");
        BasicDBObject match = GGMongoOperator.newDBObject(messSendRecord);
        //已发送
        match.put("account_id", Long.valueOf(message.getAccount_id()));
        match.put("account_name",message.getAccount_name());
        match.put("uuid", data.getUuid());
        match.put("title", data.getTitle());
        match.put("content", data.getContent());
        match.put("img_url", data.getFileAddress());
        match.put("link_type", data.getLinkType());
        match.put("link_url", data.getLinkUrl());
        match.put("mq_type", message.getMq_type());
        match.put("mq_sub_type", message.getMq_sub_type());
        match.put("product_line", Integer.valueOf(message.getProduct_line()));
        match.put("module", data.getModule());
        match.put("target", data.getTarget());
        match.put("expire_date", data.getExpireDate());//过期时间
        match.append("insert_date", new Date()); //推送时间
        match.put("status", 1);
        match.put("request_id", message.getRequest_id());
//		match.put("client_flag",message.getClient_flag());
//		match.put("sub_status",message.getSub_status());
//		match.put("pro_type",message.getPro_type());
        messSendRecord.insert(match);
    }

}
